package com.example.demojdbc01.Controller;

        import com.example.demojdbc01.Entity.User;
        import com.example.demojdbc01.Server.UserService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;

        import javax.servlet.http.HttpServletResponse;
        import java.io.IOException;
        import java.util.List;

@RestController
//该类中所有的API接口返回的数据，无论你对应的方法返回Map,List或是其他的Object,
//它会以JSON字符串的形式返回给客户端
@RequestMapping(value = "/root")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/abcd")
    //RequestMapping映射请求，通过它来指定控制器可以处理哪些URL请求
    public List<User> getUser(){
        System.out.println("你好");
        return userService.findAll();
    }
    @RequestMapping("/testfindAll")
    public List<User> getUser1(){
        return userService.findAll();
    }

    @RequestMapping(value="/hello")
    public String hello(@RequestParam("name") String name, @RequestParam("word") String word) {
        String str = "有人 " + name + " 说了一句话 " + word;
        return str;
    }

    @RequestMapping("/findOne")
    public User findOne(@RequestParam String userid){
        System.out.println("userid的值为"+userid);
        return userService.findOne(userid);}


    @RequestMapping("/findOne1/{userid}")
    public User findOne1(@PathVariable String userid){
        System.out.println("userid的值为"+userid);
        return userService.findOne(userid);}


    @RequestMapping("/delete/{userid}")
//这里的配置要和下面的一样
    public List<User> delete(@PathVariable String userid){
        System.out.println("断点1");
        //拿下面的做判断
        int t = userService.delete(userid);
        System.out.println(t);
        System.out.println("断点2");
        return userService.findAll();
    }

    @RequestMapping("/add")
    public List<User> save(@RequestParam(value = "userid") String userid,@RequestParam(value = "username") String username
    ,@RequestParam(value = "userpassword") String userpassword) {
        User user = new User(userid,username,userpassword);
        System.out.println("断点1");
        userService.save(user);
        System.out.println("断点2");
        System.out.println("");
        return userService.findAll();

    }

}